//
//  ICMBalanceRemoveFraction.m
//  ICrEAM
//
//  Created by ysw on 2016/11/26.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMBalanceRemoveFraction.h"
#import "ICMBottomBarView.h"
#import "ICMTitleTextFeildView.h"

#define displayWidth ActualWidth(405)

@interface ICMBalanceRemoveFraction () <ICMBottomBarViewDelegate>
@property (nonatomic, strong) TPKeyboardAvoidingScrollView *bigScrollView;
@property (nonatomic, strong) ICMTitleTextFeildView *fractionView;  //去零框
@property (nonatomic, strong) ICMTitleTextFeildView *amountView;    //总金额框

@end

@implementation ICMBalanceRemoveFraction

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = BackgroundColor_Tanslucent_black;
        UITapGestureRecognizer *backgroundGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeView)];
        [self addGestureRecognizer:backgroundGesture];
        self.bigScrollView = [[TPKeyboardAvoidingScrollView alloc]initWithFrame:frame];
        self.bigScrollView.backgroundColor = [UIColor clearColor];
        
        UIView *mainDisplayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, displayWidth, ActualHeight(380))];
        mainDisplayView.backgroundColor = [UIColor whiteColor];
        BorderColor(mainDisplayView, BorderColor_Gray_Hex);
        BorderWidth(mainDisplayView, 1.0);
        [self.bigScrollView addSubview:mainDisplayView];
        mainDisplayView.center = CGPointMake(self.width/2, self.height/2);
        //头部
        UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, displayWidth, ActualHeight(70))];
        [mainDisplayView addSubview:titleView];
        
        NSDictionary *titleDic = @{@"font":BoldNaviFont,@"color":TitleColor};
        UILabel *titleLabel = [self createLabelWithDic:titleDic text:@"去零" frame:CGRectMake(0, ActualHeight(25), displayWidth, ActualHeight(25))];
        [titleView addSubview:titleLabel];
        [titleLabel sizeToFit];
        titleLabel.center = CGPointMake(mainDisplayView.width/2, ActualHeight(70)/2);
        
        UIView *horSep1 = [[UIView alloc]initWithFrame:CGRectMake(0, ActualHeight(70)-1, displayWidth, ActualHeight(1))];
        horSep1.backgroundColor = SepColor_gray;
        [titleView addSubview:horSep1];
        
        //填写部分
        ICMTitleTextFeildView *amountView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(0, titleView.bottomEdge+ActualHeight(40), ActualWidth(220), ActualHeight(63)) title:@"应收账款金额"];
        self.amountView = amountView;
        [mainDisplayView addSubview:amountView];
        amountView.center = CGPointMake(displayWidth/2, amountView.centerY);
        
        ICMTitleTextFeildView *fractionView = [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(amountView.x, amountView.bottomEdge+ActualHeight(20), ActualWidth(220), ActualHeight(63)) title:@"去零金额"];
        fractionView.delegate=self;
        self.fractionView = fractionView;
        [mainDisplayView addSubview:fractionView];
        
        //按钮
        ICMBottomBarView *bottomBarView = [ICMBottomBarView createBottomBarViewWithTitles:@[@"确定",@"取消"] normalImages:@[[UIImage imageNamed:@"btn_save"],[UIImage imageNamed:@"btn_save"]] seletedImages:nil andFrame:CGRectMake(0, ActualHeight(380)-70, displayWidth, 70)];
        bottomBarView.delegate = self;
        [mainDisplayView addSubview:bottomBarView];
        [self addSubview:self.bigScrollView];
        
    }
    return self;
}
- (void)editableViewDidEndEdit:(ICMTitleTextFeildView *)sender{
    CGFloat reduce= [[ICrEAMUser shareICrEAMUser].balance_range floatValue] -   [sender.contentText floatValue];
    if (reduce<0) {
        
        self.fractionView.contentText=@"";
    }
}
- (UILabel *)createLabelWithDic:(NSDictionary *)dic text:(NSString *)text frame:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = dic[@"font"];
    label.textColor = dic[@"color"];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

//移除视图
- (void)removeView {
    [self removeFromSuperview];
}


#pragma mark -- ICMBottomBarViewDelegate
- (void)bottomBarViewDidSelectIndex:(NSInteger)index {
    if (index == 0) {
        if (self.fractionView.contentText) {
            CGFloat reduce= [[ICrEAMUser shareICrEAMUser].balance_range floatValue] - [self.fractionView.contentText floatValue];
            if (reduce<0) {
                [self.superview showString:@"去零金额超出基本参数设置的范围!" forSeconds:1.5];
                self.fractionView.contentText=@"";
            }
        }
        //返回
        //发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"CalculateFraction" object:self userInfo:@{@"fraction":self.fractionView.contentText}];
    } else {
        
    }
    [self removeView];
}

- (void)setAmoutStr:(NSString *)amoutStr
{
    _amoutStr = amoutStr;
    self.amountView.contentText = amoutStr;
}

@end
